package cn.itlaobing.web.dao;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.regex.Pattern;

import cn.itlaobing.web.model.UserModel;

public class UserDao {
	private DBhelper db=new DBhelper();
	public UserModel QueryOne(String query,String pwd) throws SQLException,IOException{
		String reg="^.\\d+$"; 
		ResultSet rs=null;
		if(Pattern.matches(reg, query)) {
			String sql="select * from tbl_user where telephone=? and pwd=?";			
			rs=db.executeQuery(sql,query,pwd);
		}else {
			String sql="select * from tbl_user where counts=? and pwd=?";			
			rs=db.executeQuery(sql,query,pwd);			
		}
		UserModel um=new UserModel();
		if(rs.next()) {
			um.setId(rs.getInt(1));
			um.setCounts(rs.getString("counts"));
			um.setLasttime(rs.getString("lasttime"));
			um.setNicheng(rs.getString("nicheng"));
			um.setPwd(rs.getString("pwd"));
			um.setTelephone(rs.getString("telephone"));
		}
		return um;
	}
	public int updateTime(Date nowTime,Integer id) throws SQLException,IOException{
		String sql="update tbl_user set lasttime=? where id=?";
		return db.executeUpdate(sql,nowTime,id);
	}
	public void close() throws SQLException{
		db.close();
	}
}
